package org.lee.controller;

import org.lee.bean.Order;
import org.lee.service.OrderService;
import org.lee.service.XAOrderService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.io.IOException;
import java.sql.SQLException;

/**
 * @author lizhe
 */
@RestController
@RequestMapping("/order")
public class OrderController {

    @Autowired
    private OrderService service;



    @PostMapping("/add")
    public int add(@RequestBody Order record) {
        return service.add(record);
    }

    @DeleteMapping("/{orderId:.+}")
    public int delete(@PathVariable("orderId") Long orderId, @RequestParam(value = "userId", required = false) Integer userId) {
        return service.delete(orderId, userId);
    }

    @PostMapping("/update")
    public int update(@RequestBody Order order) {
        return service.update(order);
    }

    @GetMapping("/{orderId:.+}")
    public Order findOne(@PathVariable("orderId") Long orderId, @RequestParam(value = "userId", required = false) Integer userId) {
        return service.findOne(orderId, userId);
    }

    @GetMapping("/xa-test")
    public void xaTest() throws SQLException, IOException {
        XAOrderService orderService = new XAOrderService("/META-INF/sharding-databases-tables.yaml");
        orderService.init();
        orderService.insert();
//        orderService.cleanup();
    }

}
